using System;
using System.Collections.Generic;
using BasicScript;
using Game.Const;
using Game.Core;
using Game.Core.Loader;
using Game.Main.Controller;
using Game.Main.Event;
using Game.Manager;
using UnityEngine;

public class Client : MonoBehaviour
{
	public static Client Instance;

	private AssetBundleManager abManager;

	private AudioManager audioManager;

	private UIManager uiManager;

	private EffectManager effectManager;

	private ConfigManager configManager;

	private CameraManager cameraManager;

	private GamePoolManager prefabPoolManager;

	//private PurchasingManager purchasingManager;

	private SaveManager saveManager;

	private DepthManager depthManager;

	private List<ResourceLoader> loaders = new List<ResourceLoader>();

	private SceneController sceneClr;

	private void Awake()
	{
		Instance = this;
	}

	private void Start()
	{
		Init();
	}

	private void Init()
	{
		InitResolution();
		InitBugly();
		Screen.sleepTimeout = 300;
		//Application.targetFrameRate = 60;
		audioManager = Singleton<AudioManager>.Instance;
		uiManager = Singleton<UIManager>.Instance;
		effectManager = Singleton<EffectManager>.Instance;
		abManager = Singleton<AssetBundleManager>.Instance;
		cameraManager = Singleton<CameraManager>.Instance;
		saveManager = Singleton<SaveManager>.Instance;
		prefabPoolManager = Singleton<GamePoolManager>.Instance;
		depthManager = Singleton<DepthManager>.Instance;
		//purchasingManager = Singleton<PurchasingManager>.Instance;
		TextConst.InitLanguage();
		abManager.Bind(LoadEvent.LOAD_COMPLETE, OnABManagerInit);
		abManager.Init();
	}

	private void OnABManagerInit(BaseEvent evt)
	{
		abManager.UnBind(LoadEvent.LOAD_COMPLETE, OnABManagerInit);
		Singleton<ConfigManager>.Instance.Bind("game.parse_config", OnParseConfig);
		Singleton<ConfigManager>.Instance.Init();
	}

	private void OnParseConfig(BaseEvent evt)
	{
		Singleton<ConfigManager>.Instance.UnBind("game.parse_config", OnParseConfig);
		InitController();
	}

	private void InitController()
	{
		sceneClr = new SceneController();
	}

	private void Update()
	{
		if (Time.frameCount % 3000 == 0)
		{
			GC.Collect();
		}
		if (sceneClr != null)
		{
			sceneClr.Update();
		}
		if (loaders == null || loaders.Count <= 0)
		{
			return;
		}
		int num = ((ClientConfig.PLATFORM != PlatformType.IOS) ? (12 - abManager.loadingResNum) : (10 - abManager.loadingResNum));
		bool flag = false;
		for (int i = 0; i < loaders.Count && num > i; i++)
		{
			flag = true;
			if (loaders[i] != null)
			{
				loaders[i].BeginLoad();
			}
            loaders[i] = null;
		}
		if (!flag)
		{
			return;
		}
		for (int num2 = loaders.Count - 1; num2 >= 0; num2--)
		{
			if (loaders[num2] == null)
			{
				loaders.RemoveAt(num2);
			}
		}
	}

	public void LateUpdate()
	{
		if (sceneClr != null)
		{
			sceneClr.LateUpdate();
		}
	}

	private void InitBugly()
	{
		BuglyAgent.InitWithAppId("b4e6e902cd");
		BuglyAgent.EnableExceptionHandler();
	}

	private void InitResolution()
	{
		if (ClientConfig.PLATFORM != PlatformType.Android)
		{
			return;
		}
		int width = Screen.currentResolution.width;
		int height = Screen.currentResolution.height;
		int num = 1280;
		int num2 = 720;
		if (width > num && height > num2)
		{
			float num3 = (float)num / (float)num2;
			float num4 = (float)width / (float)height;
			if (num3 < num4)
			{
				num = Mathf.CeilToInt((float)num2 * num4);
			}
			else if (num3 > num4)
			{
				num2 = Mathf.FloorToInt((float)num / num4);
			}
			Screen.SetResolution(num, num2, true);
		}
	}

	public void AddResourceLoaderLoader(ResourceLoader loader)
	{
		loaders.Add(loader);
	}

	private void OnApplicationFocus(bool focus)
	{
		if (sceneClr != null && !focus)
		{
			sceneClr.ReleaseAllTouch();
			sceneClr.SaveSceneArchive();
		}
	}

	private void OnDestroy()
	{
	}
}
